package com.txc.books.controller;

import com.txc.books.entity.Book;
import com.txc.books.entity.BookType;
import com.txc.books.exception.BaseException;
import com.txc.books.service.IBookTypeService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import org.springframework.web.bind.annotation.*;

/**
 * @author: XCTANG
 *
 */

@RestController
@RequestMapping("/type")
public class BookTypeController {

    private IBookTypeService BookTypeService;
    public BookTypeController(IBookTypeService BookTypeService){
        this.BookTypeService = BookTypeService;
    }

    //查找全部图书类型
    @GetMapping(value = "/findAll")
    public void findAll(){
        BookTypeService.findAll();
    }

    //添加图书类型信息
    @PostMapping(value = "/insert")
    public void addType(BookType bookType){
        try{
            BookTypeService.insert(bookType);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }

    //更新图书类型信息
    @PostMapping(value = "/update")
    public void updateType(BookType bookType){

        try{
            BookTypeService.update(bookType);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }

    //删除图书类型信息
    @GetMapping(value = "/delete")
    public void deleteType(@RequestParam("id") String id){
        BookTypeService.delete(Integer.parseInt(id.trim()));
    }

    //分页查询
    @PostMapping(value="/findPage")
    public PageResult findPage(@RequestBody PageRequest pageQuery) {

        return BookTypeService.findByPager(pageQuery);
    }
}
